﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using System.Text.RegularExpressions;
using System.Globalization;
using University.Workshop.DataAccess;
using System.Collections.ObjectModel;

namespace University.Workshop.Business
{
    public static class OfferManager
    {
        public static Offer MakeAOffer(long productId, long buyerId, string offerPrice, DateTime offerDate,string message)
        {
            string numbers = "^[0-9]*[.]?[0-9]+$";
            
            if (String.IsNullOrEmpty(offerPrice))
            {
                throw new ValidationException("You must enter a price");
            }
            if(!Regex.IsMatch(offerPrice,numbers))
            {
                throw new ValidationException("Invalid Amount");
            }
            Offer offer = new Offer();
            offer.RelatedProduct = ProductDataAccess.GetProductById(productId);
            offer.BuyerId = buyerId;
            offer.OfferPrice = float.Parse(offerPrice, CultureInfo.CurrentCulture);
            offer.OfferDate = offerDate;
            offer.Status = "Pending";
            offer.Message = message;
            try
            {
                OfferDataAccess.InsertOffer(offer);
            }
            catch (Exception)
            {
                   throw new BusinessException("Error making a offer");
            }
            return offer;
        }
        public static Collection<Offer> GetOffersByBuyerId(long buyerId)
        {
            Collection<Offer> offerList=new Collection<Offer>();
            offerList = OfferDataAccess.GetOfferByBuyerId(buyerId);
            return offerList;
        }
        public static Collection<Offer> GetOffers(long product)
        {
            Collection<Offer> offerList = new Collection<Offer>();
            offerList = OfferDataAccess.GetOffer(product);
            return offerList;
        }
        public static Offer ModifyOffer(Offer offer, string secondOffer, string status, bool condition)
        {
            if (offer != null)
            {
                if (!condition)
                {
                    string numbers = "^[0-9]*[.]?[0-9]+$";
                    if (String.IsNullOrEmpty(secondOffer))
                    {
                        throw new ValidationException("This field is required!");
                    }
                    if (!Regex.IsMatch(secondOffer, numbers))
                    {
                        throw new ValidationException("Invalid Amount");
                    }
                }

                offer.SecondOffer = float.Parse(secondOffer, CultureInfo.CurrentCulture);
                offer.Status = status;
                try
                {
                    OfferDataAccess.ModifySecondOffer(offer);
                }
                catch (Exception ex)
                {
                    throw new BusinessException(ex.Message);
                }
            }
            return offer;
        }
        public static Offer GetOneOffer(long buyerId, long productId)
        {
            Offer offer = new Offer();
            offer = OfferDataAccess.GetASingleOffer(buyerId, productId);
            return offer;
        }
    }   
}
